package zmee.pur.src.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;

import java.util.Date;
import java.util.EventObject;

/**
 * @Author: xw
 * @Date: 2022/11/29/14:22
 * @Description: 限制投标截标时间大于系统时间且在【20-30】天期间内
 */
public class ProjectBaseBillPlugin extends AbstractBillPlugIn {

    /**
     * 投标截止时间
     */
    public static final String KEY_STOPBIDDATE = "stopbiddate";

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        IFormView parentView = this.getView().getParentView();
        if (parentView != null) {
            DynamicObject dataEntity = parentView.getModel().getDataEntity();
            //项目信息.寻源方式.number
            String sourceTypeNumber = dataEntity.getDynamicObject("sourcetype").getString("number");
            if ("C010405".equals(sourceTypeNumber)) {
                String proName = e.getProperty().getName();
                if (KEY_STOPBIDDATE.equals(proName)) {
                    ChangeData[] changeSet = e.getChangeSet();
                    Object newValue = changeSet[0].getNewValue();
                    if (null == newValue) {
                        return;
                    }
                    //投标截止时间
                    Date stopBidDate = (Date) newValue;
                    Date nowDate = new Date();
                    long stopBidDateTime = stopBidDate.getTime();
                    long nowDateTime = nowDate.getTime();
                    long time = Math.abs(stopBidDateTime - nowDateTime);
                    int day = (int) (time / 1000 / 60 / 60 / 24);
                    if (day < 20 || day > 30) {
                        this.getView().showMessage("投标截止时间距当前时间不足20天或已超过30天，请重新设置！");
                        this.getModel().setValue(KEY_STOPBIDDATE, "");
                    }
                }
            }
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        this.getModel().setValue("tendency", "1");
    }
}